package com.neutech.variable;

import java.util.Arrays;

/**
 * 数组的扩容和缩容（常用，基于数组复制）
 * @author Neuedu-远哥
 * @version 1.0
 * @date 2021/11/10 16:30
 */
public class Demo14 {
    public static void main(String[] args) {
        int[] arr1 = {1,2,3,4,5};
        // 把6放进数组中
        /*int[] arr2 = new int[arr1.length+1];
        System.arraycopy(arr1,0,arr2,0,arr1.length);
        System.out.println("arr2:" + Arrays.toString(arr2));
        arr2[arr2.length-1] = 6;
        System.out.println("arr2:" + Arrays.toString(arr2));*/
        // JDK提供的方法
        arr1 = Arrays.copyOf(arr1,arr1.length - 1);
//        arr1[arr1.length-1] = 6;
//        arr1[0] = -100;
        System.out.println(Arrays.toString(arr1));

    }
}
